home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / clean / sun3.lha / Sun3 / seqdemos / mulmat.icl < prev    next >
Text File  |  1992-08-07  |  3KB  |  92 lines

  1. MODULE mulmat;
  2.  
  3. <<
  4. Matrix Multiplication.
  5.  
  6. This  program  performs  matrix multiplication  on matrices of  integers.
  7. The  initial matrices  (Mat1 & Mat2) can have  arbitrary size (Size). The
  8. second matrix is transposed  first, in order to avoid traversing a matrix
  9. by column, which is very inefficient. The result of the program shows the
  10. initial  matrices  and the  resulting matrix.
  11. >>
  12.  
  13. IMPORT deltaI;
  14.  
  15.  
  16. TYPE
  17.  
  18. ::  Row     -> [INT];   == A row is a list of integers.
  19. ::  Col     -> [INT];   == A column is a list of integers.
  20. ::  Mat     -> [Row];   == A matrix is a list of rows.
  21. ::  TMat    -> [Col];   == A transposed matrix is a list of columns.
  22. ::  Index   -> INT;     == An index is an integer.
  23.  
  24.  
  25. MACRO
  26.  
  27.     Size -> 6;  == The size of the matrices.
  28.  
  29.  
  30. RULE
  31.  
  32. ==  The initial matrices
  33.  
  34. ::  Mat1 -> Mat;
  35.     Mat1 -> [[  1, 2, 3, 4, 5, 6 ]      == 
  36.             ,[  0, 1, 2, 3, 4, 5 ]      == 
  37.             ,[ -1, 0, 1, 2, 3, 4 ]      ==  The product of these matrices:
  38.             ,[ -2,-1, 0, 1, 2, 3 ]      == 
  39.             ,[ -3,-2,-1, 0, 1, 2 ]      == 
  40.             ,[ -4,-3,-2,-1, 0, 1 ]];    ==      [ 0 -9  0  5  1  7 ]
  41.                                         ==      [ 0 -8 -1  4  1  6 ]
  42. ::  Mat2 -> Mat;                        ==      [ 0 -7 -2  3  1  5 ]
  43.     Mat2 -> [[  0, 1, 0, 0, 0,-1 ]      ==      [ 0 -6 -3  2  1  4 ]
  44.             ,[  1, 0, 1, 1, 0, 1 ]      ==      [ 0 -5 -4  1  1  3 ]
  45.             ,[ -1, 0, 1,-1, 0, 0 ]      ==      [ 0 -4 -5  0  1  2 ]
  46.             ,[ -1,-1,-1, 0,-1, 0 ]      == 
  47.             ,[  1, 0, 1, 0, 1, 0 ]      == 
  48.             ,[  0,-1,-1, 1, 0, 1 ]];    == 
  49.  
  50.  
  51. ==  Multiplying two matrices.
  52.  
  53. ::  MulMat Mat Mat -> Mat;
  54.     MulMat m1 m2   -> TMulMat m1 (Transpose m2);
  55.  
  56. ::  TMulMat Mat TMat  -> Mat;
  57.     TMulMat [r] m2    -> [ MulRow r m2 ];
  58.     TMulMat [r|rs] m2 -> [ MulRow r m2 | TMulMat rs m2 ];
  59.  
  60. ::  MulRow Row TMat -> Row;
  61.     MulRow r [c]    -> [ Inprodukt r c ];
  62.     MulRow r [c|cs] -> [ Inprodukt r c | MulRow r cs ];
  63.  
  64. ::  Inprodukt Row Col       -> INT;
  65.     Inprodukt [] []         -> 0;
  66.     Inprodukt [a|as] [b|bs] -> + (* a b) (Inprodukt as bs);
  67.  
  68.  
  69. ==  Transposing a matrix.
  70.  
  71. ::  Transpose Mat -> TMat;
  72.     Transpose m   -> Transp m 1;
  73.  
  74. ::  Transp Mat Index -> TMat;
  75.     Transp m i  -> [ Column m i ]                  , IF = i Size
  76.                 -> [ Column m i | Transp m (++ i) ];
  77.  
  78. ::  Column Mat Index -> Col;
  79.     Column [] i      -> [];
  80.     Column [r|rs] i  -> [ Select r i | Column rs i ];
  81.  
  82. ::  Select Row Index -> INT;
  83.     Select [a|as] 1  -> a;
  84.     Select [a|as] i  -> Select as (-- i);
  85.  
  86.  
  87. ==  The Start rule: show the initial matrices and their product.
  88.  
  89. ::  Start -> (Mat,STRING,Mat,STRING,Mat);
  90.     Start -> (m1,"\ntimes\n",m2,"\nbecomes\n",MulMat m1 m2),
  91.              m1: Mat1, m2: Mat2;
  92.